from flask import Blueprint,request,jsonify
from flask_restful import Api,Resource
from unit.db import db1
from unit.comm import get_data
import datetime
from views.rs import rs1

task=Blueprint('task',__name__)
# api=Api(task)

# class TaskS(Resource):
#     def post(self):
#         data=get_data()
#         title=data['title']
#         statime=data['statime']
#         endtime=data['endtime']
#         type=data['type']
#         content=data['content']
#         print(data)
#         sql="insert into msg(title,statime,endtime,type,content) values('{}','{}','{}',{},'{}')".format(title,statime,endtime,int(type),content)
#         db1.update(sql)


#         return 'ok'

#     def get(self):
#         sql="select * from msg"
#         res=db1.find_all(sql)
#         return jsonify({'code':200,'list':res})


# api.add_resource(TaskS,'/task')

@task.route('/noticem',methods=['POST'])
def noticem():
    data = get_data()
    print(data)
    list = []
    for (i, value) in data.items():
        if i[:-3] == 'values':
            list.append(int(value))
    now = datetime.datetime.strftime(datetime.datetime.now(),'%Y-%m-%d %H:%M:%S')
    # 添加通知表
    sql = "insert into notice(title,notice_type,content,type,level,ctime) values('{}',{},'{}',{},{},'{}')".format(data['title'],int(data['notice_type']),data['content'],int(data['type']),int(data['level']),now)
    id = db1.update(sql)
    if int(data['type']) == 3:
        for i in list:
            sql1 = "insert into notice_record(notice_id,user_id,ctime) values({},{},'{}')".format(int(id),int(i),now)
            db1.update(sql1)

    return jsonify({'code':200,'msg':'添加成功'})

@task.route('/getnotice')
def getnotice():
    userid = request.args.get('id')
    sql = "select * from notice where type=1 or id in(select notice_id from notice_record where user_id={}) order by ctime desc,level asc".format(int(userid))
    res = db1.findall(sql)
    mes = []

    for i in res:
        i['status'] = 0
        # 消息发给所有人
        if int(i['type']) == 1:
            # 点击进入详情页,在详情页里更新redis set
            key = 'notice'+str(i['id'])
            # 此条消息的已读列表
            list = rs1.get_all_hash(key)
            if list:
                if userid in list:
                    i['status'] = 1
        if int(i['type']) == 3:
            sql2 = "select * from notice_record where notice_id={} and user_id={}".format(int(i['id']),int(userid))
            res1 = db1.find(sql2)
            if res1:
                i['status'] = 1
        mes.append(i)
    return jsonify({'code':200,'lsit':mes})


@task.route('/updatenotice',methods=['POST'])
def updatenotice():
    # 获取信息
    data = get_data()
    # 查看消息类型,如果消息类型为1,想redis中添加数据
    sql = "select * from notice where id={}".format(int(data['noticeid']))
    notice = db1.find(sql)
    if int(notice['type']) == 1:
        key = 'notice' + str(notice['id'])
        rs1.get_value_hash(key,int(data['user_id']))
    # 如果消息为3 更新数据库
    if int(notice['type']) == 3:
        sql = "update notice_record set status=1 where notice_id={} and user_id={}".format(int(data['notice_id']),int(data['user_id']))
        db1.update(sql)
    return jsonify({'code':200})